package com.example.demo.controller;

import com.example.demo.entity.MenuEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.result.ResponseResult;
import com.example.demo.service.MenuService;
import com.example.demo.service.UserService;
import com.example.demo.utils.LoginUtil;
import com.example.demo.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private LoginUtil loginUtil;
    @PostMapping("/register")
    @ResponseBody
    public ResponseResult<?> register(@RequestBody UserVo userVo) {
        UserEntity register = userService.register(userVo);
        return ResponseResult.success(register);
    }
    @PostMapping("/login")
    @ResponseBody
    public ResponseResult<UserVo> login(@RequestBody UserVo userVo) {
        String token = userService.login(userVo);
        UserVo res = new UserVo();
        res.setToken(token);
        return ResponseResult.success(res);
    }
    @PostMapping("/info")
    @ResponseBody
    public ResponseResult<UserEntity> info() {
        UserEntity login = loginUtil.getLogin();

        return ResponseResult.success(login);
    }

    @PostMapping("/logout")
    @ResponseBody
    public ResponseResult<Boolean> logout() {
        Boolean login = loginUtil.logout();
        return ResponseResult.success(login);
    }


    @PostMapping("/menu")
    @ResponseBody
    public ResponseResult<?> menu() {
        UserEntity login = loginUtil.getLogin();
        //todo 还要设置一个权限组 目前菜单栏来源此接口
         List<MenuEntity> menuEntity = menuService.findByUserId(login.getId());

        return ResponseResult.success(menuEntity);
    }


    @PostMapping("/findAllUser")
    @ResponseBody
    public ResponseResult<?> findAllUser() {

        List<UserEntity> allUser = userService.findAllUser();

        return ResponseResult.success(allUser);
    }




}
